package com.changgou.goods.controller;


import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import com.google.common.reflect.ClassPath;
import com.netflix.discovery.converters.Auto;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")

@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;


    //查询全部数据
    @GetMapping
    public Result<Brand> findAll(){


        List<Brand> brandList = brandService.findAll();


        return new Result<Brand>(true, StatusCode.OK,"查询成功",brandList);
    }


    //id查询品牌
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable Integer id){

        Brand brand = brandService.findById(id);

        return  new Result<Brand>(true,StatusCode.OK,"查询成功",brand);
    }

    //新增品牌数据数据
    @PostMapping
    public Result save(@RequestBody Brand brand){


         brandService.save(brand);


        return new Result<Brand>(true, StatusCode.OK,"新增成功");
    }


    //修改品牌数据数据
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable Integer id){

        brand.setId(id);

        brandService.update(brand);


        return new Result<Brand>(true, StatusCode.OK,"修改成功");
    }


    //删除品牌数据数据
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id){



        brandService.delete(id);


        return new Result<Brand>(true, StatusCode.OK,"删除成功");
    }


    //条件查询品牌列表

    @PostMapping("/search")
    public Result<Brand> findList(@RequestBody Brand brand){

        //List<Brand>  与 Result<List<Brand>> z作用相同
        List<Brand> list = brandService.findList(brand);


        return  new Result<>(true,StatusCode.OK,"条件查询成功",list);

    }


    //分页查询
    //page :第几页的页数
    //size :每页显示数量
    @GetMapping("/search/{page}/{size}")
    public  Result<Brand> findPage(@PathVariable Integer page,@PathVariable Integer size){

        //PageInfo<Brand>  与 Result<List<Brand>> z作用相同
       PageInfo<Brand> pageInfo = brandService.findPage(page,size);
        return  new Result<>(true,StatusCode.OK,"分页查询成功",pageInfo);

    }


    //分页条件查询
    //page :第几页的页数
    //size :每页显示数量
    @PostMapping("/search/{page}/{size}")
    public  Result<Brand> findPage(@PathVariable Integer page,@PathVariable Integer size,@RequestBody(required = false) Brand brand){

        int i = 1/0;

        //PageInfo<Brand>  与 Result<List<Brand>> z作用相同
        PageInfo<Brand> pageInfo = brandService.findPage(page,size,brand);
        return  new Result<>(true,StatusCode.OK,"分页查询成功",pageInfo);

    }
}
